//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using System.ComponentModel;

namespace OpenLS.Chart
{
    public class DataLabels : ViewModelBase, IOfficeXmlSerializable, IShape
    {
        internal const string xmlName = "dLbls";
        private ExtensionElement _extension;
        private bool delete;
        private List<DataLabel> dLbl;
        private DataLabelPosition dLblPos;
        private ChartLines leaderLines;


        private NumericFormat numFmt;
        private string separator;
        private bool? showBubbleSize;
        private bool? showCatName;
        private bool showLeaderLines;
        private bool? showLegendKey;
        private bool? showPercent;
        private bool? showSerName;
        private bool? showVal = true;

        private ShapeProperties spPr;

        private TextBody txPr;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; notifyPropertyChanged("Extension");}
        }

        public NumericFormat Format
        {
            get { return numFmt; }
            set { numFmt = value; notifyPropertyChanged("Format"); }
        }

        public ShapeProperties ShapeProperties
        {
            get { return spPr; }
            set { spPr = value; notifyPropertyChanged("ShapeProperties"); }
        }

        public TextBody TextProperties
        {
            get { return txPr; }
            set { txPr = value; notifyPropertyChanged("TextProperties"); }
        }

        public DataLabelPosition Position
        {
            get { return dLblPos; }
            set { dLblPos = value; notifyPropertyChanged("Position");}
        }

        public bool? LegendKeyVisible
        {
            get { return showLegendKey; }
            set { showLegendKey = value; notifyPropertyChanged("LegendKeyVisible");}
        }

        public bool? ValueVisible
        {
            get { return showVal; }
            set { showVal = value; notifyPropertyChanged("ValueVisible"); }
        }

        public bool? CategoryNameVisible
        {
            get { return showCatName; }
            set { showCatName = value; notifyPropertyChanged("CategoryNameVisible"); }
        }

        public bool? SeriesNameVisible
        {
            get { return showSerName; }
            set { showSerName = value; notifyPropertyChanged("SeriesNameVisible"); }
        }

        public bool? PercentVisible
        {
            get { return showPercent; }
            set { showPercent = value; notifyPropertyChanged("PercentVisible"); }
        }

        public bool? BubbleSizeVisible
        {
            get { return showBubbleSize; }
            set { showBubbleSize = value; notifyPropertyChanged("BubbleSizeVisible"); }
        }

        public string Separator
        {
            get { return separator; }
            set { separator = value; notifyPropertyChanged("Separator"); }
        }


        public ChartLines LeaderLines
        {
            get { return leaderLines; }
            set { leaderLines = value; notifyPropertyChanged("LeaderLines"); }
        }


        public bool LeaderLinesVisible
        {
            get { return showLeaderLines; }
            set { showLeaderLines = value; notifyPropertyChanged("LeaderLinesVisible"); }
        }


        public bool Deleted
        {
            get { return delete; }
            set { delete = value; notifyPropertyChanged("Deleted"); }
        }

        public List<DataLabel> Labels
        {
            get { if (dLbl == null)
                dLbl = new List<DataLabel>();
                return dLbl; }
           // private set { dLbl = value; notifyPropertyChanged("Labels"); }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read(DataLabels.xmlName))
            {
                var v = c.ReadOptionalWhile<DataLabel>("dLbl");
                if (v != null)
                    Labels.AddRange(v);
                //Labels = c.ReadOptionalWhile<DataLabel>("dLbl");
                if (c.Reader.LocalName == "delete")
                {
                    using (ReadContext cc = c.Read("delete"))
                    {
                        Deleted = cc.GetOptionalBoolean("val", true);
                    }
                }
                else
                {
                    //read EG_DLblShared
                    numFmt = c.ReadOptionalElement<NumericFormat>("numFmt");
                    spPr = c.ReadOptionalElement<ShapeProperties>("spPr");
                    txPr = c.ReadOptionalElementAs<TextBody>("txPr");
                    dLblPos = c.ReadOptionalElementEnumAttribute("dLblPos", "val",
                                                                                    DataLabelPosition.BestFit,
                                                                                    DataLabelPosition.BestFit);
                        //\\default?
                    showLegendKey = c.ReadOptionalElementBooleanAttribute("showLegendKey");
                    showVal = c.ReadOptionalElementBooleanAttribute("showVal");
                    showCatName = c.ReadOptionalElementBooleanAttribute("showCatName");
                    showSerName = c.ReadOptionalElementBooleanAttribute("showSerName");

                    showPercent = c.ReadOptionalElementBooleanAttribute("showPercent");
                    showBubbleSize = c.ReadOptionalElementBooleanAttribute("showBubbleSize");

                    separator = c.ReadOptionalElementString("separator");
                    if (c.ReaderLocalName == "showLeaderLines")
                    {
                        using (ReadContext cc = c.Read("showLeaderLines"))
                        {
                            LeaderLinesVisible = cc.GetOptionalBoolean("val", true);
                        }
                    }
                    LeaderLines = c.ReadOptionalElementAs<ChartLines>("leaderLines");
                }
                c.Eat("txPr");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(DataLabels.xmlName))
            {
                if (this.dLbl != null)
                    foreach (IOfficeXmlSerializable item in Labels)
                        item.WriteXml(c);
                if (Deleted)
                    c.WriteOptionalElementBooleanAttribute("delete", Deleted);
                else
                {
                    //Write EG_DLblShared
                    c.WriteOptionalElement(numFmt);
                    c.WriteOptionalElement(spPr);
                    c.WriteOptionalElementAs(txPr, "txPr");
                    c.WriteOptionalElementEnumAttribute("dLblPos", "val", dLblPos,
                                                                           DataLabelPosition.BestFit,
                                                                           DataLabelPosition.BestFit); //\\default?
                    c.WriteOptionalElementBooleanAttribute("showLegendKey", showLegendKey);
                    c.WriteOptionalElementBooleanAttribute("showVal", showVal);
                    c.WriteOptionalElementBooleanAttribute("showCatName", showCatName);
                    c.WriteOptionalElementBooleanAttribute("showSerName", showSerName);

                    c.WriteOptionalElementBooleanAttribute("showPercent", showPercent);
                    c.WriteOptionalElementBooleanAttribute("showBubbleSize", showBubbleSize);

                    c.WriteOptionalString("separator", separator);
                    c.WriteOptionalElementBooleanAttribute("showLeaderLines", LeaderLinesVisible);
                    c.WriteOptionalElementAs(LeaderLines, "leaderLines");
                }
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

    }
}